package com.blog.blog.controller;

import com.blog.blog.service.LoginService;
import com.blog.blog.service.SysUserService;
import com.blog.blog.vo.ErrorCode;
import com.blog.blog.vo.Result;
import com.blog.blog.vo.params.LoginParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static com.blog.blog.utils.CommunityConstant.DEFAULT_EXPIRED_SECONDS;
import static com.blog.blog.utils.CommunityConstant.REMEMBER_EXPIRED_SECONDS;

@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private SysUserService userService;



//    @PostMapping
//    public Result login(@RequestBody LoginParams loginParams) {
//        return loginService.login(loginParams);
//    }

    @PostMapping
    public Result login(@RequestBody LoginParams loginParams, HttpServletResponse response) {
        //记住我
        boolean rememberMe = true;
        String account = loginParams.getAccount();
        String password = loginParams.getPassword();
        int expiredSeconds = rememberMe ? REMEMBER_EXPIRED_SECONDS : DEFAULT_EXPIRED_SECONDS;
        return userService.login(account, password, expiredSeconds, response);
    }
}
